//
#import "BKSignatures.h"
#import "BKCFType.h"
#import "BKConstant.h"
#import "BKEnum.h"
#import "BKFunction.h"
#import "BKStringConstant.h"
#import "BKStruct.h"

@implementation BKSignatures

+ (id) signaturesWithXMLElement: (NSXMLElement*) elem framework: (BKFramework*) fw
{
	BKSignatures* sigs = [[[BKSignatures alloc] init] autorelease];
	sigs.framework = fw;
	
	NSArray* elems;
	if(elems = [elem elementsForName: @"struct"])
	{
		NSMutableArray* structs = [NSMutableArray array];
		for(NSXMLElement* e in elems)
		{
			//[structs addObject: [[[BKStruct alloc] initWithXMLElement: e signatures: sigs] autorelease]];
			BKStruct* b = [[[BKStruct alloc] initWithXMLElement: e signatures: sigs] autorelease];
			b.type;
			[structs addObject: b];
		}
		sigs.structs = structs;
	}
	else if(elems = [elem elementsForName: @"opaque"])
	{
		NSMutableArray* structs = [NSMutableArray array];
		for(NSXMLElement* e in elems)
		{
			BKStruct* b = [[[BKStruct alloc] initWithXMLElement: e signatures: sigs] autorelease];
			b.opaque = YES;
			[structs addObject: b];
		}
		sigs.opaques = structs;
	}
	if(elems = [elem elementsForName: @"cftype"])
	{
		NSMutableArray* cftypes = [NSMutableArray array];
		for(NSXMLElement* e in elems)
		{
			[cftypes addObject: [[[BKCFType alloc] initWithXMLElement: e signatures: sigs] autorelease]];
		}
		sigs.cftypes = cftypes;
	}
	if(elems = [elem elementsForName: @"constant"])
	{
		NSMutableArray* constants = [NSMutableArray array];
		for(NSXMLElement* e in elems)
		{
			[constants addObject: [[[BKConstant alloc] initWithXMLElement: e signatures: sigs] autorelease]];
		}
		sigs.constants = constants;
	}
	if(elems = [elem elementsForName: @"string_constant"])
	{
		NSMutableArray* constants = [NSMutableArray array];
		for(NSXMLElement* e in elems)
		{
			[constants addObject: [[[BKStringConstant alloc] initWithXMLElement: e signatures: sigs] autorelease]];
		}
		sigs.constants = constants;
	}
	if(elems = [elem elementsForName: @"enum"])
	{
		NSMutableArray* enums = [NSMutableArray array];
		for(NSXMLElement* e in elems)
		{
			[enums addObject: [[[BKEnum alloc] initWithXMLElement: e signatures: sigs] autorelease]];
		}
		sigs.enums = enums;
	}
	if(elems = [elem elementsForName: @"function"])
	{
		NSMutableArray* functions = [NSMutableArray array];
		for(NSXMLElement* e in elems)
		{
			[functions addObject: [[[BKFunction alloc] initWithXMLElement: e signatures: sigs] autorelease]];
		}
		sigs.functions = functions;
	}
	
	return sigs;
}

- (id) init
{
	self = [super init];
	return self;
}

- (void) dealloc
{
	[_dependencies release];
	[_structs release];
	[_cftypes release];
	[_opaques release];
	[_constants release];
	[_stringConstants release];
	[_enums release];
	[_functions release];
	[_functionAliases release];
	[_informalProtocols release];
	[_classes release];
	[super dealloc];
}

- (BKFunction*) functionWithName: (NSString*) name
{
	BKFunction* func;
	for(func in _functions)
	{
		if([func.name isEqual: name])
			return func;
	}
	return nil;
}

@synthesize framework = _framework;
@synthesize structs = _structs;
@synthesize opaques = _opaques;
@synthesize cftypes = _cftypes;
@synthesize constants = _constants;
@synthesize stringConstants = _stringConstants;
@synthesize enums = _enums;
@synthesize functions = _functions;

@end
